import cv2
import os
import time
import argparse

def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument('--path',help='video path')
    parser.add_argument('--outpath', default='./output', help='output path')
    parser.add_argument('--fps', default=5,type=int, help='frames per sencond')
    parser.add_argument('--split', default=0,type=int, help='split')
    args = parser.parse_args()
    return args

def get_frame(path,outpath,fps,split=None):
    if not  os.path.exists(outpath):
        os.makedirs(outpath)
    if split>0:
        for i in range(split):
            os.makedirs(outpath+'/'+str(i))
   
    cap = cv2.VideoCapture(path)
    ret,frame = cap.read()
    orifps = cap.get(cv2.CAP_PROP_FPS)
    interval = int(orifps/fps)
    index = 0
    while ret:
        if index % interval==0:
            if split>0:
                cv2.imwrite(outpath+'/'+str(index%split)+'/'+str(time.time())+'.jpg',frame)
            else:
                cv2.imwrite(outpath+'/'+str(time.time())+'.jpg',frame)
        index +=1
        ret , frame = cap.read()    

if __name__ == '__main__':
    args = parse_args()
    
    get_frame(args.path,args.outpath,args.fps,args.split)
